iT邦幫忙

2023 iThome 鐵人賽

DAY 25
0
Web 3

I have an AI ,I have a React, Ugh ,AI -React 系列 第 25

[Day]要收假了 文件整理器

  • 分享至 

  • xImage
  •  

把旅遊攻略 跟照片整理一下

咒語: 設計一個文件管理器頁面,使用 Material-UI 的樹狀列表、表格和圖標組件,讓用戶管理文件和文件夾。

import React, { useState } from "react";
import {
  List,
  ListItem,
  ListItemIcon,
  ListItemText,
  Collapse,
} from "@mui/material";
import {
  Folder as FolderIcon,
  InsertDriveFile as FileIcon,
  ExpandLess,
  ExpandMore,
} from "@mui/icons-material";

const initialFolders = const initialFolders = [
  {
    id: "folder-1",
    name: "旅行",
    type: "folder",
    children: [
      {
        id: "file-1",
        name: "旅遊攻略.pdf",
        type: "file",
      },
      {
        id: "file-2",
        name: "旅遊照片.jpg",
        type: "file",
      },
      {
        id: "file-3",
        name: "地圖.jpg",
        type: "file",
      },
    ],
  },
];

const FileManager = () => {
  const [folders, setFolders] = useState(initialFolders);
  const [openFolders, setOpenFolders] = useState([]);

  const toggleFolder = (folderId) => {
    if (openFolders.includes(folderId)) {
      setOpenFolders(openFolders.filter((id) => id !== folderId));
    } else {
      setOpenFolders([...openFolders, folderId]);
    }
  };

  return (
    <div>
      <h1>文件管理器</h1>
      <List>
        {folders.map((folder) => (
          <div key={folder.id}>
            <ListItem button onClick={() => toggleFolder(folder.id)}>
              <ListItemIcon>
                <FolderIcon />
              </ListItemIcon>
              <ListItemText primary={folder.name} />
              {openFolders.includes(folder.id) ? <ExpandLess /> : <ExpandMore />}
            </ListItem>
            <Collapse in={openFolders.includes(folder.id)}>
              <List component="div" disablePadding>
                {folder.children.map((child) => (
                  <ListItem key={child.id} button>
                    <ListItemIcon>
                      <FileIcon />
                    </ListItemIcon>
                    <ListItemText primary={child.name} />
                  </ListItem>
                ))}
              </List>
            </Collapse>
          </div>
        ))}
      </List>
    </div>
  );
};

export default FileManager;

這樣一個可以整理資料的小系統就完成了


上一篇
[Day24] you Got mail
下一篇
[Day 26] 開工了 來看看一下新聞
系列文
I have an AI ,I have a React, Ugh ,AI -React 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言